<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>Three框架</title>
    <script src="../thingjs/js/three.js"></script>
    <script src="../thingjs/js/Stats.js"></script>
    <style type="text/css">
        div#canvas-frame {
            border: none;
            cursor: pointer;
            width: 100%;
            height: 600px;
            background-color: #EEEEEE;
        }

    </style>
</head>

<body onload="threeStart();">
<div id="canvas-frame"></div>
</body>

<script>

    var renderer;
    var stats;
    function initThree() {
        width = document.getElementById('canvas-frame').clientWidth;
        herght = document.getElementById('canvas-frame').clientHeight;
        renderer = new THREE.WebGLRenderer({
            antialias : true
        });
        renderer.setSize(width, herght);
        document.getElementById('canvas-frame').appendChild(renderer.domElement);
        renderer.setClearColor(0xFFFFFF, 1.0);

        stats = new Stats();
        stats.domElement.style.position = 'absolute';
        stats.domElement.style.left = '0px;';
        stats.domElement.style.top = '10px';
        document.getElementById('canvas-frame').appendChild(stats.domElement);
    }

    var camera;
    function initCamera() {
        camera = new THREE.PerspectiveCamera(45, width / herght, 1, 10000);
        camera.position.x = 0;
        camera.position.y = 0;
        camera.position.z = 600;
        camera.up.x = 0;
        camera.up.y = 1;
        camera.up.z = 0;
        camera.lookAt({
            x : 0,
            y : 0,
            z : 0
        });
    }

    var scene;
    function initScene() {
        scene = new THREE.Scene();
    }

    var light;
    function initLight() {
        light = new THREE.AmbientLight(0xFFFFFF);
        light.position.set(100, 100, 200);
        scene.add(light);
        light = new THREE.PointLight(0x00FF00);
        light.position.set(0, 0, 300);
        scene.add(light);
    }

    function initObject(){
        var geometry = new THREE.CylinderGeometry(100, 150, 400);
        var material = new THREE.MeshLambertMaterial({
           color : 0xFFFF00
        });
        var mesh = new THREE.Mesh(geometry, material);
        mesh.position = new THREE.Vector3(0, 0, 0);
        scene.add(mesh);
    }

    //使用相机将场景渲染到页面上去
    function threeStart() {
        initThree();    //实例化对象
        initCamera();   //相机
        initScene();    //场景
        initLight();    //亮
        initObject();   //创建对象，赋初值
        animation();    //渲染
    }

    function animation() {
        camera.position.x = camera.position.x + 1;
        renderer.render(scene, camera); //重新渲染
        requestAnimationFrame(animation);
        stats.update();//更新状态
    }

</script>

</html>